import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import axios from 'axios'
export default new Vuex.Store({
  state: {
    address: []
  },
  getters: {
    currAddress(state) {
      return state.address.find(v => v.isChecked)
    }
  },
  mutations: {
    SET_ADDRESS(state, payload) {
      state.address = payload
    },
    ADD_ADDRESS(state, payload) {
      state.address.unshift(payload)
    },
    SET_DEFAULT_ADDRESS(state, payload) {
      state.address.forEach(v => {
        if(v.id === payload.id) {
          Vue.set(v, 'isChecked', true)
        } else {
          Vue.set(v, 'isChecked', false)
        }
      })
    }
  },
  actions: {
    async GET_ADDRESS({ commit }) {
      const resp = await axios.get('/api/address');
      commit('SET_ADDRESS', resp.data.data)
    }
  },
  modules: {
  }
})
